package com.lingnan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.SelfActive;
import com.lingnan.pojo.User;
import com.lingnan.service.SelfActiveService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 个人活动表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/self-active")
public class SelfActiveController {

    @Autowired
    private SelfActiveService selfActiveService;

    /**
     * 添加我的活动
     * @param selfActive
     * @return
     */
    @PostMapping("/addSelfActive")
    @ApiOperation("添加我的活动")
    public Result addSelfActive(@ApiParam("传个人活动对象")@RequestBody SelfActive selfActive){

        if (!ObjectUtils.isEmpty(selfActive)){
            QueryWrapper<SelfActive> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",selfActive.getId());
            SelfActive one = selfActiveService.getOne(queryWrapper);
            if (!ObjectUtils.isEmpty(one)){
                selfActiveService.addSelfActive(selfActive);
                return ResultGenerator.genSuccessResult(200,"我的活动添加成功！");
            }
            return ResultGenerator.genErrorResult(202,"该活动已添加！");
        }
        return ResultGenerator.genErrorResult(201,"我的活动添加失败！");
    }

    /**
     * 查询个人的活动
     * @param userVo
     * @return
     */
    //y
    @PostMapping("/findAll")
    public Result findAllSelfActive(@ApiParam("传用户no")@RequestBody UserVo userVo){
        List<SelfActive> list = null;
        if (!ObjectUtils.isEmpty(userVo)){
            QueryWrapper<SelfActive> wrapper = new QueryWrapper<>();
            wrapper.eq("no",userVo.getNo());
            list = selfActiveService.list(wrapper);
        }
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"我的活动展示成功！");
        }
        return ResultGenerator.genErrorResult(201,"我的活动展示失败！");
    }



}

